// 清单js
var $scope = null;
$(function() {
	var appElement = document.querySelector('[ng-controller=main]');
	$scope = angular.element(appElement).scope();
	$scope.methods.province();
	initialPage($scope);
	initPaginator(1);
});
// 初始化值
function initialPage($scope) {
	$scope.search.bridgeType = 'XDCSSG'; //默认古代消失
	$scope.search.province = '';
	$scope.search.city = '';
	$scope.search.county = '';
	$scope.province='';
	$scope.county='';
	$scope.city='';
	$scope.methods.checkList(1);
}

function initPaginator(page) {
	var optionsInit = {
		currentPage: 1, //当前的请求页数。
		totalPages: 1, //一共多少页。
		size: "normal", //应该是页眉的大小。
		bootstrapMajorVersion: 3, //bootstrap的版本要求。
		alignment: "right",
		numberOfPages: 5, //一页列出多少数据。
	};
	$("#pageLimit").bootstrapPaginator(optionsInit);
}
var mainApp = angular.module("mainApp", []);
mainApp.controller('main', function($scope) {
	$scope.search = {};
	console.log($scope.search)
	$scope.methods = {
		ensure: function() {
			$scope.search.province=$('#province option:selected').text();
			$scope.search.county=$('#county option:selected').text();
			$scope.search.city=$('#city option:selected').text();
			$scope.methods.checkList(1);
		},
		// 省市区
		province: function() {
			$.ajax({
				url: './static/js/pca-code.json',
				type: 'get',
				dataType: 'JSON',
				async: true, //同步方式发起请求
				success: function(data) {
					if (data != null) {
						$scope.data = data; //
						$scope.$apply();
					} else {

					}

				},

			})
		},
		// 清除
		clear: function() {
			initialPage($scope)
		},
		// 下面动态列表
		checkList: function(page) {
			var params = $scope.methods.paramSearch();
			params += '&pageNum=' + page
			params += '&pageSize=6'
			$.ajax({
				url: $.shysUrlPrefix + '/website/checkList?' + params,
				type: 'get',
				dataType: 'JSON',
				async: true, //同步方式发起请求
				beforeSend: function() {
					$(".loading").show();
				},
				success: function(data) {
					if (data.rows != null && data.rows.length > 0) {
						$('.state-news').show();
						$('.widget-kong').hide();
						$scope.checkList = data.rows;
						$scope.$apply();
						var totalPages = Math.ceil(data.total / 6);
						if (totalPages < 2) {
							totalPages = 1;
						}
						var options = {
							currentPage: page, //当前的请求页数。
							totalPages: totalPages, //一共多少页。
							size: "normal", //应该是页眉的大小。
							bootstrapMajorVersion: 5, //bootstrap的版本要求。
							alignment: "right",
							numberOfPages: 5, //显示按钮的数量。
							itemTexts: function(type, page,
								current) { //如下的代码是将页眉显示的中文显示我们自定义的中文。
								switch (type) {
									case "first":
										return "首页";
									case "prev":
										return "上一页";
									case "next":
										return "下一页";
									case "last":
										return "尾页";
									case "page":
										return page;
								}
							},
							onPageClicked: function(event, originalEvent, type,
								page
								) { //给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
								console.log(page)
								$scope.methods.checkList(page);



							}
						};
						$("#pageLimit").bootstrapPaginator(options);
					} else {
						$('.state-news').hide();
						$('.widget-kong').show();
					}

				},
				complete: function() {
					$(".loading").hide();
				},

			})
		},
		//param参数
		paramSearch: function() {
			var params = '';
			if ($scope.search.bridgeType != undefined ) {
				params += '&bridgeType=' + $scope.search.bridgeType
			}
			if ($scope.search.province != undefined && $scope.search.province != '') {
				params += '&province=' + $scope.search.province
			}
			if ($scope.search.city != undefined && $scope.search.city != '') {
				params += '&city=' + $scope.search.city
			}
			if ($scope.search.county != undefined && $scope.search.county != '') {
				params += '&county=' + $scope.search.county
			}
			return params
		},
		// 跳转详情页
		labelDetailClick: function(label) {
			console.log(label.checkListId)
			window.location.href = "account-detail.html?checkListId=" + label.checkListId
		},

	};
});
